package com.core.zyyz.amc.modules.service.impl;

import java.beans.FeatureDescriptor;
import java.util.*;
import java.util.stream.Stream;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.core.common.utils.StringUtils;
import com.core.zyyz.amc.modules.domain.AmcSystemMainlistD;
import com.core.zyyz.amc.modules.domain.AmcSystemMainlistJSON;
import com.core.zyyz.amc.modules.domain.AmcSystemMainsortD;
import com.core.zyyz.amc.modules.mapper.AmcSystemMainlistDMapper;
import com.core.zyyz.amc.modules.mapper.AmcSystemMainsortDMapper;
import com.core.zyyz.amc.modules.service.AmcSystemMainlistDService;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static com.alibaba.fastjson.JSON.parseArray;
import static com.alibaba.fastjson.JSON.parseObject;

/**
 * 主页信息配置Service业务层处理
 *
 * @author core
 * @date 2024-11-13
 */
@Service
public class AmcSystemMainlistDServiceImpl implements AmcSystemMainlistDService {

    @Autowired
    private AmcSystemMainlistDMapper amcSystemMainlistDMapper;
    @Autowired
    private AmcSystemMainsortDMapper amcSystemMainsortDMapper;

    /**
     * 查询功能页面配置主列表
     *
     * @param paramMap 功能页面配置主
     * @return 功能页面配置主
     */
    @Override
    public List<AmcSystemMainlistD> queryMainConfigList(AmcSystemMainlistD paramMap) {
        return amcSystemMainlistDMapper.queryMainConfigList(paramMap);
    }

    /**
     * 新增主页信息配置
     *
     * @param paramMap 主页信息配置
     * @return 结果
     */
    @Override
    public int insertAmcSystemMainListD(Map<String, Object> paramMap) {
        String baseBusinessNode = MapUtils.getString(paramMap, "baseBusinessNode");
        int count = 1;
        /*保存主页信息配置*/
        JSONArray mainList = JSONArray.parseArray(paramMap.get("mainList") + "");
        List<AmcSystemMainlistD> dataList = JSONObject.parseArray(mainList.toJSONString(), AmcSystemMainlistD.class);
        for (int i = 0; i < dataList.size(); i++) {
            dataList.get(i).setMainlistId(UUID.randomUUID().toString());
            dataList.get(i).setBaseBusinessNode(baseBusinessNode);
            dataList.get(i).setMainlistNum((long) (i + 1));
            count = amcSystemMainlistDMapper.insertAmcSystemMainlistD(dataList.get(i));
        }

        /*保存主页排序信息配置*/
        JSONArray sortList = JSONArray.parseArray(paramMap.get("sortList") + "");
        List<AmcSystemMainsortD> dataList2 = JSONObject.parseArray(sortList.toJSONString(), AmcSystemMainsortD.class);
        for (int i = 0; i < dataList2.size(); i++) {
            dataList2.get(i).setMainsortId(UUID.randomUUID().toString());
            dataList2.get(i).setBaseBusinessNode(baseBusinessNode);
            dataList2.get(i).setMainsortNum((long)(i + 1));
            count = amcSystemMainsortDMapper.insertAmcSystemMainsortD(dataList2.get(i));
        }
        return count;
    }

    /**
     * 修改主页信息配置
     *
     * @param paramMap 主页信息配置
     * @return 结果
     */
    @Override
    public int updateAmcSystemMainListD(Map<String, Object> paramMap) {
        String baseBusinessNode = MapUtils.getString(paramMap, "baseBusinessNode");

        //删除主页信息配置明细行
        amcSystemMainlistDMapper.deleteMainListByBusinessNode(baseBusinessNode);
        //删除主页排序配置明细行
        amcSystemMainsortDMapper.deleteMainSortByBusinessNode(baseBusinessNode);

        int count = this.insertAmcSystemMainListD(paramMap);
//        /*保存主页信息配置*/
//        JSONArray mainList = JSONArray.parseArray(paramMap.get("mainList") + "");
//        List<AmcSystemMainlistD> dataList = JSONObject.parseArray(mainList.toJSONString(), AmcSystemMainlistD.class);
//        for (int i = 0; i < dataList.size(); i++) {
//            dataList.get(i).setMainlistId(UUID.randomUUID().toString());
//            dataList.get(i).setBaseBusinessNode(baseBusinessNode);
//            dataList.get(i).setMainlistNum((long) (i + 1));
//            amcSystemMainlistDMapper.insertAmcSystemMainlistD(dataList.get(i));
//        }
//
//        /*保存主页排序信息配置*/
//        JSONArray sortList = JSONArray.parseArray(paramMap.get("sortList") + "");
//        List<AmcSystemMainsortD> dataList2 = JSONObject.parseArray(sortList.toJSONString(), AmcSystemMainsortD.class);
//        for (int i = 0; i < dataList2.size(); i++) {
//            dataList2.get(i).setMainsortId(UUID.randomUUID().toString());
//            dataList2.get(i).setBaseBusinessNode(baseBusinessNode);
//            dataList2.get(i).setMainsortNum((long)(i + 1));
//            amcSystemMainsortDMapper.insertAmcSystemMainsortD(dataList2.get(i));
//        }
        return count;
    }

    /**
     * 查询功能页面配置主列表
     *
     * @param baseBusinessNode 功能页面配置主
     * @return 功能页面配置主
     */
    @Override
    public List<AmcSystemMainlistJSON> queryMainListNode(String baseBusinessNode) {
        List<AmcSystemMainlistD> result = null;
        List<AmcSystemMainlistJSON> resultJson = new ArrayList<AmcSystemMainlistJSON>();
        if(StringUtils.isNotEmpty(baseBusinessNode)){
            result = amcSystemMainlistDMapper.queryMainConfigListNode(baseBusinessNode);
        }
        if (null != result) {
            for (AmcSystemMainlistD mainList : result){
                AmcSystemMainlistJSON mainListJSON = new AmcSystemMainlistJSON();
                this.copyNonNullPropertiesStatic(mainList,mainListJSON);
                mainListJSON.setEventsAttr(parseArray(mainList.getEventsAttr()));
                mainListJSON.setOtherAttr(parseObject(mainList.getOtherAttr()));
                if (mainListJSON.getEventsAttr() == null) {
                    mainListJSON.setEventsAttr(new JSONArray());
                }
                if (mainListJSON.getOtherAttr() == null) {
                    mainListJSON.setOtherAttr(new JSONObject());
                }
                resultJson.add(mainListJSON);
            }
        }
        return resultJson;
    }

    /**
     * 查询功能页面配置主列表排序
     *
     * @param baseBusinessNode 功能页面配置主
     * @return 功能页面配置主
     */
    @Override
    public List<AmcSystemMainsortD> querySortListNode(String baseBusinessNode) {
        List<AmcSystemMainsortD> result = null;
        if(StringUtils.isNotEmpty(baseBusinessNode)){
            result = amcSystemMainsortDMapper.querySortConfigListNode(baseBusinessNode);
        }
        return result;
    }

    /**
     * 批量删除主页配置数据
     *
     * @param baseBusinessNodes 需要删除的主页信息配置主键
     * @return 结果
     */
    @Override
    public int deleteAllDetails(String[] baseBusinessNodes) {
        List<String> baseBusinessNodeList = Arrays.asList(baseBusinessNodes);
        int count = 0;
        for (String baseBusinessNode : baseBusinessNodeList){
            //删除主页信息配置明细行
            count = amcSystemMainlistDMapper.deleteMainListByBusinessNode(baseBusinessNode);
            //删除主页排序配置明细行
            count = amcSystemMainsortDMapper.deleteMainSortByBusinessNode(baseBusinessNode);
        }
        return count;
    }


    /**
     * @Description：将一个实体类转换为另一个实体类
     *
     * @ChangeInfomation:
     */
    public static void copyNonNullPropertiesStatic(Object source, Object target) {
        final BeanWrapper wrappedSource = new BeanWrapperImpl(source);
        String[] nullPropertyNames = Stream.of(wrappedSource.getPropertyDescriptors())
                .map(FeatureDescriptor::getName)
                .filter(propertyName -> wrappedSource.getPropertyValue(propertyName) == null)
                .toArray(String[]::new);
        BeanUtils.copyProperties(source, target, nullPropertyNames);
    }

}
